package cn.team.controller;

import cn.team.Util.MailUtil;
import cn.team.entity.Order;
import cn.team.service.ApplayService;
import cn.team.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ApplayService applayService;
    @Resource
    MailSender mailSender;

    // 插入订单
    @PostMapping("/insert")
    public void insertOrder(@RequestBody Order order) {
        orderService.insertOrder(order);
    }

    // 根据 parent 字段查询订单
    @GetMapping("/byParent")
    public List<Order> getByParent(@RequestParam String parent) {
        return orderService.selectByParent(parent);
    }
    @GetMapping("/byParentAndValid")
    public List<Order> getByParentAndVaild(@RequestParam String parent) {
        return orderService.selectByParentAndVaild(parent);
    }

    // 根据 tutor 字段查询订单
    @GetMapping("/byTutor")
    public List<Order> getByTutor(@RequestParam String tutor) {
        return orderService.selectByTutor(tutor);
    }

    // 根据 shi 和 xian 字段进行模糊查询订单
    @GetMapping("/byShiXianLike")
    public List<Order> getByShiXianLike(
            @RequestParam String shi,
            @RequestParam String xian) {
        return orderService.selectByShiXianLike(shi, xian);
    }

    // 根据 id 删除订单
    @DeleteMapping("/delete/{id}")
    public void deleteById(@PathVariable int id) {
        orderService.deleteById(id);
    }

    // 根据日期小于等于 startDate 且 isOrder 值为 0 的有效预约查询
    @GetMapping("/validAppointments")
    public List<Order> getValidAppointments(@RequestParam("startDate")String date,@RequestParam("username")String username,
                                            @RequestParam("shi")String shi) {
        System.out.println(date);
        System.out.println(shi);
        return orderService.selectValidAppointments(date,username,shi.substring(0, shi.length() - 1));
    }

    // 根据日期大于 endDate 或 isOrder 值为 1 的无效预约查询
    @GetMapping("/invalidAppointments")
    public List<Order> getInvalidAppointments(@RequestParam("endDate")String date,@RequestParam("username")String username) {
        System.out.println(date);


        return orderService.selectInvalidAppointments(date,username);
    }

    // 根据 id 修改 comment
    @PostMapping("/updateComment/{id}")
    public void updateComment(@PathVariable int id) {
        orderService.updateCommentById(id);
    }

    @GetMapping("/getOrderById/{id}")
    public Order getOrderById(@PathVariable int id){
        return orderService.getOrderById(id);
    }

    @GetMapping("/orderSuccess")
    public int orderSuccess(@RequestParam("id")int id,@RequestParam("tutor")String tutor){
        orderService.orderSuccess(id,tutor);
        Order order = orderService.getOrderById(id);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        String msg = "家长:"+order.getParent()+"同意了您申请"+order.getStartDate()+"至"+order.getEndDate()+
                "的"+order.getGrade()+"的"+order.getSubject()+"的兼职申请";
        MailUtil.sendMailMessage(mailSender,simpleMailMessage,"您有兼职申请成功请到程序查看",msg,tutor);
        applayService.applaySuccess("***",id,tutor);
        return 0;
    }

    @GetMapping("/orderFail")
    public int orderFail(@RequestParam("id")int id,@RequestParam("tutor")String tutor){
        applayService.applayFail(id,tutor);
        return 0;
    }

    @GetMapping("/getWork")
    public List<Order> getRenwu(@RequestParam("username")String username,@RequestParam("type")String type) {
        if (Objects.equals(type, "tutor")) {
            return orderService.getWork(username);
        } else {
            return orderService.getRenWu(username);

        }

    }
    @GetMapping("/cords")
    public List<Order> getCords(@RequestParam("username")String username){
        return orderService.getCords(username);
    }

    @GetMapping("/updateComment")
    public void updateComment(@RequestParam("id")int id,@RequestParam("comment")String comment){
        orderService.updateComment(id,comment);
    }
    @GetMapping("/deleteCord")
    public void deleteCord(@RequestParam("id")int id){
        orderService.deleteCord(id);
    }

    @GetMapping("/quitRenwu")
    public void quitRenwu(@RequestParam("id")int id) {
        orderService.quitRenwu(id);
    }
    @GetMapping("/quXiaoRenwu")
    public void quXiaoRenwu(@RequestParam("id")int id) {
        orderService.quXiaoRenwu(id);
        applayService.quXiaoRenwu(id);
    }
    @GetMapping("/searchOrders")
    public List<Order> searchOrders(@RequestParam String keyword) {
        return orderService.searchOrders(keyword);
    }

    @GetMapping("/getComments")
    public List<Order> getCommemts(@RequestParam("tutor") String tutor) {
        return orderService.getCommengts(tutor);
    }
}
